package com.zws.cucumber.bootstrap;

import java.util.HashMap;
import java.util.Map;

public class ZwsBootstrapEnvsBuilder {

    private Map<String, String> originalEnvMap;
    private Map<String, String> overrideEnvMap;

    public static ZwsBootstrapEnvsBuilder builder() {
        return new ZwsBootstrapEnvsBuilder();
    }

    private ZwsBootstrapEnvsBuilder() {
        this.originalEnvMap = new HashMap<>(System.getenv());
        this.overrideEnvMap = new HashMap<>();
    }

    public ZwsBootstrapEnvsBuilder withEnv(String key, String value) {
        this.overrideEnvMap.put(key, value);
        return this;
    }

    public ZwsBootstrapEnvsBuilder withEnv(Map<String, String> envMap) {
        this.overrideEnvMap.putAll(envMap);
        return this;
    }

    public Map<String, String> toMap() {
        Map<String, String> envMap = new HashMap<>(originalEnvMap);
        envMap.putAll(overrideEnvMap);
        return envMap;
    }

    public String[] toArray() {
        Map<String, String> envMap = toMap();
        String[] envArray = new String[envMap.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : envMap.entrySet()) {
            envArray[i++] = entry.getKey() + "=" + entry.getValue();
        }
        return envArray;
    }
}
